/**
 * 
 */
package stationGUI;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

import javax.swing.JPanel;

/**
 * @author Ian Gardner
 * @version 1.0
 *
 */
public class NeighborhoodScreen extends JPanel {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  
  public static final int NEIGHBOR_HEIGHT = 50;
  public static final int NEIGHBOR_WIDTH = 60;  
  public static final int ME_HEIGHT = 70 ;
  public static final int ME_WIDTH = 100;
  public static final BasicStroke OUTLINE_SIZE = new BasicStroke(1.5f);
  public static final BasicStroke LINE_SIZE = new BasicStroke(1.1f);
  public static final BasicStroke WARNING_LINE_SIZE = new BasicStroke(4.5f);
  public static final Color LINE_COLOR = Color.BLACK;
  public static final double NEIGHBOR_RING_FACTOR = .8;
  public static final boolean DISPLAY_NEIGHBOR_COUNT = true;
  public static final Font FONT = new Font("Times New Roman", Font.BOLD, 26);
  public static final String ME_DISPLAY = "YOU";
  public static final int ME_FLAG = 0;

  
  //logistic vars
  int midw, midh;
  private Toolkit toolkit;
  Dimension size;
  FontMetrics fm;
  int fh, fw;
  Ellipse2D e = new Ellipse2D.Double();
  Line2D l = new Line2D.Double();
  ClientNode node;
  NodeBean me;
  
  /**
   * 
   */
  public NeighborhoodScreen(ClientNode n) {
    setBackground(Station.BACKGROUND_COLOR);
    setLayout(new BorderLayout());
    
    //set my prefered size to a sqaure
    toolkit = Toolkit.getDefaultToolkit();
    size = toolkit.getScreenSize();
    size.width = size.height;
    setPreferredSize(size);
    
      
    
    //set my node
    me=n.me;
    n.neighborhood=this;
  }
    
  public void paint(Graphics g2) {
    Graphics2D g=(Graphics2D)g2;
    
    //get the size of the panel
    size=this.getSize();
    midw=(size.width)/2;
    midh=(size.height)/2;
    
    //set the font info
    g.setFont(FONT);
    fm = g.getFontMetrics();
    fh=fm.getHeight();
    
    //paint all the nodes
    NodeBean current;
    int tot=me.numNeighbors;
    for(int i=0;i<tot;i++) {
      current=me.neighbors.get(i);
      paintNode(g,current.color,(int)(midw-NEIGHBOR_RING_FACTOR*midw*Math.cos((double)(i)/tot*2*Math.PI)),(int)(midh-NEIGHBOR_RING_FACTOR*midh*Math.sin((double)(i)/tot*2*Math.PI)),current.numNeighbors);
    }
    
    paintNode(g,me.color,midw,midh,ME_WIDTH,ME_HEIGHT,ME_FLAG);
  }
  
  
  /**
   * Paints every node. Finds the right spot to keep the node centered given
   * the parameters. Prints a neighbor amount if wanted.
   * 
   * @param g - the graphics instance to print with
   * @param c - the color of the current node
   * @param x - the x coordinate of the middle of the node
   * @param y - the y coordinate of the middle of the node
   * @param w - the w of the enclosing rectangle
   * @param h - the h of the enclosing rectangle
   * @param n - the number of neighbors of this node. ME_FLAG for current
   */
  public void paintNode(Graphics2D g,Color c,int x,int y,int w,int h,Integer n){
    
    //clear the old line
    g.setColor(getBackground());
    g.setStroke(WARNING_LINE_SIZE);
    l.setLine(midw,midh,x,y);
    g.draw(l);
    
    //change to the correct line size
    if(!c.equals(me.color)) g.setStroke(LINE_SIZE);
    g.setColor(LINE_COLOR);
    g.draw(l);
    
    //draw the node and outline
    g.setStroke(OUTLINE_SIZE);
    e.setFrame(x-w/2,y-h/2,w,h);
    g.setColor(c);
    g.fill(e);
    g.setColor(LINE_COLOR);
    g.draw(e);
    
    //show your label and neighbor count label
    if(n==0) {
      g.setColor(Color.WHITE);
      fw=fm.stringWidth(ME_DISPLAY);
      g.drawString(ME_DISPLAY,x-fw/2,y+fh*2/7);
    } 
    else if(DISPLAY_NEIGHBOR_COUNT) {
      String s=n.toString();
      g.setColor(Color.WHITE);
      fw=fm.stringWidth(s);
      g.drawString(s,x-fw/2,y+fh*2/7);
    }    
  }
  
  /**
   * Calls paint node with the generic width and height of neighbor nodes.
   * 
   * @param g - the graphics instance to print with
   * @param c - the color of the current node
   * @param x - the x coordinate of the middle of the node
   * @param y - the y coordinate of the middle of the node
   * @param w - the w of the enclosing rectangle
   * @param h - the h of the enclosing rectangle
   * @param n - the number of neighbors of this node. ME_FLAG for current
   */
  public void paintNode(Graphics2D g,Color c,int x,int y,int n){
    paintNode(g,c,x,y,NEIGHBOR_WIDTH,NEIGHBOR_HEIGHT,n);
  }
}